package com.example.clooweather.util;

import android.text.TextUtils;
import android.util.Log;

import com.example.clooweather.db.City;
import com.example.clooweather.db.County;
import com.example.clooweather.db.Province;
import com.example.clooweather.gson.WeatherBean;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 李明洋~ on 2018/3/12.
 */

public class Utility {
    //解析和处理服务器返回的省级数据
    public static boolean handleProvinceResponse(String response) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allProvinces = new JSONArray(response);
                //将服务器返回的数据传入到JSONArray对象中然后循环遍历这个JSONArray
                for (int i = 0; i < allProvinces.length(); i++) {
                    //从中取出的每一个元素都是JSONObject对象
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    //每一个JSONObject对象中又会包含一些数据 只需要调用getXXX()方法取出
                    //接着组装成实体类对象
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));
                    //最后调用save()方法将数据储存到数据库
                    province.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean handleCityResponse(String response, int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCities = new JSONArray(response);
                for (int i = 0; i < allCities.length(); i++) {
                    JSONObject cityObject = allCities.getJSONObject(i);
                    City city = new City();
                    city.setCityName(cityObject.getString("name"));
                    city.setCityCode(cityObject.getInt("id"));
                    city.setProvinceId(provinceId);
                    city.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean handleCountyResponse(String response, int cityId) {
        if (!TextUtils.isEmpty(response)) {
            try {
                JSONArray allCounties = new JSONArray(response);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countyObject = allCounties.getJSONObject(i);
                    County county = new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /*
    * 将JSON数据解析成Weather实体类
    * */
    public static WeatherBean handleWeatherBeanResponse(String response) {
        try {
            Gson gson = new Gson();
            WeatherBean weatherBean = gson.fromJson(response, WeatherBean.class);
            return weatherBean;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
